package com.hut.oa.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hut.oa.model.bean.NoteDir;
import com.hut.oa.model.bean.Notes;
import com.hut.oa.model.bean.User;
import com.hut.oa.model.dao.note.NoteDao;
import com.hut.oa.model.dao.note.NoteDirDao;
import com.hut.oa.model.dao.user.UserDao;
import com.hut.oa.model.mapper.NotesMapper;
import com.hut.oa.service.NoteService;

import freemarker.template.utility.StringUtil;

@Controller
@RequestMapping("/note")
public class NoteController {

	@Autowired
	private NoteDirDao notedirdao;
	
	@Autowired
	private NoteDao notedao;
	
	@Autowired
	private UserDao udao;
	
	@Autowired
	private NotesMapper notemapper;
	
	@Autowired
	private NoteService noteser;
	@RequestMapping
	public String toNote(HttpSession session,Model model){
		User u=(User)session.getAttribute("userinfo");
		List<NoteDir> notedir=notedirdao.findByUser(u);
		if(notedir.size()==0){
			NoteDir dir=new NoteDir("第一个笔记本",u);
			notedirdao.save(dir);
		}
		List<Notes> list=notedao.findByUserIdAndIsDelete(u, 0);
		model.addAttribute("notedata",list);
		model.addAttribute("notesize",list.size());
		//model.addAttribute("typetittle", "全部笔记");
		List<NoteDir> notedirbyname=notedirdao.findByUserAndName(u,"第一个笔记本");
		model.addAttribute("notedirbyname", notedirbyname);
		model.addAttribute("userlist", udao.findByIdNot(u.getId()));
		return "note/note";
	}
	
	/**
	 * 到笔记查看页面
	 * @return
	 */
	@RequestMapping("/read")
	public String toAddFile(){
		return "note/noteRead";
	}
	
	/**
	 * 新建笔记
	 * @return
	 */
	@RequestMapping("/newnote")
	public String newnote(HttpSession session,Model model){
		User u=(User)session.getAttribute("userinfo");
		List<NoteDir> notedirbyname=notedirdao.findByUserAndName(u,"第一个笔记本");
		model.addAttribute("notedirbyname", notedirbyname);
		return "note/newnote";
	}
	
	/**
	 * 新建笔记本
	 */	
	@RequestMapping("/newnotedir")
	public @ResponseBody boolean newnotedir(@RequestParam("notedirname")String name,HttpSession session){
		User u=(User)session.getAttribute("userinfo");
		NoteDir dir=new NoteDir(name,u);
		List<NoteDir> list=notedirdao.findByUserAndName(u,name);
		if(list.size()>0){
			return false;
		}else{
			notedirdao.save(dir);
			return true;
		}
			
	}
	/**
	 * 选择不同分类的笔记本
	 */
	@RequestMapping("/selectdirtype")
	public String selectdirtype(@RequestParam("dirid")String dirid,HttpSession session,Model model){
		User u=(User)session.getAttribute("userinfo");
		if(dirid.equals("all")){
			model.addAttribute("notedata", notedao.findByUserIdAndIsDelete(u, 0));		
			return "note/notedata";
		}else if(dirid.equals("share")){
			List<Map<String, Object>> list=notemapper.selectnotebyid(u.getId());
			List<Notes> notelist=new ArrayList<>();
			for (Map<String, Object> map : list) {
				notelist.add(notedao.findOne(Long.parseLong(map.get("nu_note_id")+"")));
			}
			model.addAttribute("notedata",notelist);
			return "note/notedata";
		}else if(dirid.equals("company")){			
			model.addAttribute("notedata",notedao.findByIsDeleteAndPermission(0,"1"));
			return "note/notedata";
		}else{
			NoteDir dir=new NoteDir();
			dir.setId(Integer.parseInt(dirid));	
			model.addAttribute("notedata",notedao.findByUserIdAndIsDeleteAndDir(u, 0,dir));
			return "note/notedata";
		}		
	}	
	/**
	 * 加载不同的分类
	 */
	 @RequestMapping("/loadtype")
	 public @ResponseBody List<NoteDir> loadtype(HttpSession session){
		 	User u=(User)session.getAttribute("userinfo");
			List<NoteDir> notedir=notedirdao.findByUser(u);
			return notedir;
	}	
	 /**
	  * 新建笔记
	  */
	 @RequestMapping(value="/newnotefinish",method=RequestMethod.POST)
	 @ResponseBody
	 public void newnotefinish(HttpSession session,@RequestParam("tittle")String tittle,@RequestParam("content")String content,@RequestParam("dirid")String dirid){		 
		 	User u=(User)session.getAttribute("userinfo");	
		 	Notes note=new Notes();
			NoteDir dir=new NoteDir();
			dir.setId(Integer.parseInt(dirid));
			note.setContent(content);
			note.setDir(dir);
			note.setTitle(tittle);
			note.setUpdateTime(new Date());
			note.setUserId(u);
			notedao.save(note);
	}	
	 
	 /**
	  * 笔记编辑
	  */
	 @RequestMapping(value="/notedit",method=RequestMethod.POST)
	 @ResponseBody
	 public void notedit(HttpSession session,@RequestParam("tittle")String tittle,@RequestParam("content")String content,@RequestParam("noteid")String noteid){		 
		 	User u=(User)session.getAttribute("userinfo");			 	
		 	Notes note=notedao.findOne(Long.parseLong(noteid));
			note.setContent(content);
			note.setTitle(tittle);
			note.setUpdateTime(new Date());
			notedao.save(note);
	}
	 
	 /**
	  *更换笔记本
	  */
	 @RequestMapping(value="/editnotetype",method=RequestMethod.POST)
	 public @ResponseBody Boolean editnotetype(HttpSession session,@RequestParam("dirid")String dirid,@RequestParam("noteid")String noteid){		 
		 	User u=(User)session.getAttribute("userinfo");	
		 	Notes note=notedao.findOne(Long.parseLong(noteid));		 
		 	NoteDir dir=new NoteDir();
		 	dir.setId(Integer.parseInt(dirid));
		 	note.setDir(dir);	 	
			notedao.save(note);
			return true;
	}
	 /**
	  * 收藏笔记本
	  * 
	  */	 
	 @RequestMapping(value="/updatastar",method=RequestMethod.POST)
	 @ResponseBody
	 public void updatastar(HttpSession session,@RequestParam(value="noteid",required=false)Long noteid,@RequestParam(value="type",required=false)String type){		 
		 	Notes note=notedao.findOne(noteid);
		 	if(type.equals("收藏")){
		 		note.setIsstar(1);
		 	}else{
		 		note.setIsstar(0);
		 	} 	
		 	notedao.save(note);
	}
	 /**
	  * 删除笔记
	  */
	 @RequestMapping(value="/deletenewnote",method=RequestMethod.POST)
	 
	 public String deletenewnote(HttpSession session,Model model,@RequestParam("noteid")String noteid,@RequestParam("dirid")String dirid){		 
		 	User u=(User)session.getAttribute("userinfo");	 	 	 	
		 	if(notemapper.selectnotebynoteid(Long.parseLong(noteid)).size()>0){
		 		notemapper.deletebynoteid(Long.parseLong(noteid));
		 	}
		 	notedao.delete(Long.parseLong(noteid));		
		 	if(dirid.equals("undefined")){
		 		model.addAttribute("notedata",notedao.findByUserIdAndIsDelete(u, 0));
		 	}else if(dirid.trim().equals("star")){
		 		model.addAttribute("notedata",notedao.findByUserIdAndIsDeleteAndIsstar(u,0,1));	
		 	}else{		 		
			 	NoteDir dir=new NoteDir();
				dir.setId(Integer.parseInt(dirid));	
				model.addAttribute("notedata",notedao.findByUserIdAndIsDeleteAndDir(u, 0,dir));	
		 	}		 	
			return "note/notedata";
	}
		
	 /**
	  * 星标邮件
	  */
	 @RequestMapping(value="/allnotestar")
	 public String allnotestar(HttpSession session,Model model){
		 	User u=(User)session.getAttribute("userinfo");
			model.addAttribute("notedata",notedao.findByUserIdAndIsDeleteAndIsstar(u,0,1));	
			model.addAttribute("notesize",notedao.findByUserIdAndIsDeleteAndIsstar(u,0,1).size());
			return "note/notedata";
	}
	/**
	 * 分享笔记
	 */
	 @RequestMapping(value="/sharenote")
	 @ResponseBody
	 public void sharenote(HttpSession session,Model model,@RequestParam(value="user",required=false)String[] user,@RequestParam("noteid")Long noteid,@RequestParam("company")String company){
		 Notes note=notedao.findOne(noteid);
		 List<User> list=new ArrayList<>();
		 if(null!=user){
			 for (int i = 0; i < user.length; i++) {
					User u=udao.findById(Long.parseLong(user[0]));
					list.add(u);
				} 
			 note.setNoteuser(list);			 
		 }	 
		 if(("all").equals(company)){
			 note.setPermission("1");
		 }
		 notedao.save(note);
	}
	 
	 /**
	  * 查找分享人
	  */
	 @RequestMapping(value="/sharevaluefind")
	 public @ResponseBody List<User> sharevaluefind(HttpSession session,@RequestParam(value="value",required=false)String value){
		 User u=(User)session.getAttribute("userinfo");
		 if(null==value){
			return udao.findByIdNot(u.getId());
		}else{
			return udao.findbyrealnameandnotid(value,u.getId());
		}
	 }
	 /**
	  * 删除笔记本
	  */	 
	 @RequestMapping(value="/deletenoteid")
	 @ResponseBody
	 public void deletenoteid(HttpSession session,@RequestParam(value="dirid",required=false)String dirid){
		 User u=(User)session.getAttribute("userinfo");
		 NoteDir dir=new NoteDir();
		 dir.setId(Integer.parseInt(dirid));	 
		 noteser.deletenotedir(dir, u);	
	 }
}
